﻿using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Permissions;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Security;

namespace ipi.MCC.CustomFieldTypes
{
    [Guid("6d5229df-00c5-4a1c-a7a6-1125bd739e69")]
    public class MCCTreePositionField : SPFieldMultiChoice
    {
        private string sStartNode;
        
        public MCCTreePositionField(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {
        }
        
        public MCCTreePositionField(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
        {
        }

        public string StartNode
        {
            get
            {
                return (string)this.GetCustomProperty("StartNode");
            }

            set
            {
                sStartNode = value;
            }
        }
        
        public override BaseFieldControl FieldRenderingControl
        {
            [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
            get
            {
                MCCTreePositionFieldControl fieldControl = new MCCTreePositionFieldControl();
                fieldControl.FieldName = this.InternalName;
                fieldControl.SetStartNodeID(this.StartNode);

                return fieldControl;
            }
        }

        public override void Update()
        {
            this.SetCustomProperty("StartNode", sStartNode);
            base.Update();
        }
    }
}
